package com.filter;

import com.action.UserAction;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FilterSession implements Filter {
    private FilterConfig _filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        _filterConfig = filterConfig;
    }

    public void destroy() {
        _filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                                                                                                     ServletException {
        
        HttpServletRequest req = (HttpServletRequest) request;
               HttpServletResponse res = (HttpServletResponse) response;
          
               HttpSession session = req.getSession(false);
               

               if (session == null || session.getAttribute("user") == null) {
                   res.sendRedirect(req.getContextPath() + "/faces/login.jsf"); // No logged-in user found, so redirect to login page.
               } else {
                   chain.doFilter(request, response); // Logged-in user found, so just continue request.
               }
    }
}
